/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricsnormalpanel.h"
#include "configmanager.h"

const QString& LyricsNormalPanel::Builder::getPanelName() const {
    static QString name = LyricsNormalPanel::tr("Normal");
    return name;
};

LyricsPanel* LyricsNormalPanel::Builder::create(QWidget* parent) const {
    return new LyricsNormalPanel(parent);
};

QColor LyricsNormalPanel::getFadingColor(const int timePass, const int timeLength) {
    ConfigManager* cfg = ConfigManager::getInstance();
    if (!cfg->value("Style/EnableFading", true).toBool() || timeLength == 0)
        return cfg->value("Style/HighlightColor", Qt::green).value<QColor>();
    int factor = (timePass * 2 <= timeLength) ? timePass : timeLength - timePass;
    factor = qMin(0xff, factor * 0xff / cfg->value("Style/FadingTime", 300).toInt());
    return alphaBlending(
            cfg->value("Style/HighlightColor", Qt::green).value<QColor>(),
            cfg->value("Style/BgColor", Qt::darkGreen).value<QColor>(),
            factor);
};

void LyricsNormalPanel::drawFrame(LyricsTime time) {
    LyricsData::const_iterator px = m_lyrics->find(time);
    if (px != m_lyrics->end()) {
        int timeLength = (px + 1 == m_lyrics->end()) ? 0 : (px + 1)->offset - px->offset;
        drawText(m_painter, (width() - m_painter.fontMetrics().width(px->lyrics)) / 2,
            (height() - m_painter.fontMetrics().height()) / 2 + m_painter.fontMetrics().ascent(),
            getFadingColor(time - px->offset, timeLength),
            px->lyrics);
    };
};
